package app.agent

import app.agent.scatchpad.ScratchPadAgent
import app.agent.systems.SystemsAgent
import app.comet.Subscriber
import app.server._
import im.mange.jetboot.Renderable
import im.mange.jetboot.bootstrap3.GridSystem
import im.mange.jetboot.js.JsCmdFactory
import jetboot._
import GridSystem._
import app.agent.tests.TestsAgent
import app.agent.systems.SystemsAgent
import app.server.TestsResponse
import app.agent.scatchpad.ScratchPadAgent
import app.server.ServicesResponse
import app.server.StartedTestRun
import app.agent.tests.TestsAgent
import app.server.SystemsResponse
import app.agent.activity.ActivityAgent
import app.agent.finder.FinderAgent
import app.agent.events.EventsAgent
import JsCmdFactory._
import app.Mode

//TODO: consider feeds being a ticker style ...
case class RootAgent(subscriber: Subscriber) extends Renderable {
  private val finderAgent = FinderAgent(subscriber)
  private val activityAgent = ActivityAgent(subscriber)
  private val eventsAgent = EventsAgent()

  override def render = <form class="lift:form.ajax"><br/>{layout.render}</form>

  private def layout = container(
    //TODO: later will be finderAgent
    if (Mode.isCompressed) row(col(3, finderAgent), col(6, activityAgent), col(3, eventsAgent)) else //,
    row(col(5, finderAgent), col(4, activityAgent), col(3, eventsAgent))//,
//    row(col(4, testsAgent /*testPacks, releases, queue? runs?*/))//,
//    row(col(9, scratchPadAgent)),
//    row(col(3, systemsAgent))
    //row(edit, result, queue?, run?)
  )

  //TODO: should this be a comet.SystemsResponse here?
  def onSystemsResponse(response: SystemsResponse) = finderAgent.onShowResponse(response)

  //TODO: should this be a comet.ServicesResponse here?
  def onServicesResponse(response: ServicesResponse) = finderAgent.onShowResponse(response)

  //TODO: should be ListTests() or GetTests() or something
  //TODO: should this be a comet.TestsResponse here?
  def onTestsResponse(response: TestsResponse) = finderAgent.onShowResponse(response)

  def onTestContent(response: TestContent) = {
    activityAgent.onTestContent(response) & (if (Mode.isCompressed) finderAgent.hide else nothing)
  }

  //TODO: should this be a comet.StartedTestRun here?
  //TODO: should this be sent a consoleAgent?
  def onValidatedTestRun(response: ValidatedTestRun) = activityAgent.onValidatedTestRun(response)
  def onStartedTestRun(response: StartedTestRun) = activityAgent.onStartedTestRun(response)
  def onStoppedTestRun(response: StoppedTestRun) = activityAgent.onStoppedTestRun(response)
  def onUpdateTestRun(response: UpdateTestRun) = activityAgent.onUpdateTestRun(response)

  def onAddSystem = activityAgent.onAddSystem

  def onCleanup() {}
}
